<!--设备监控页面-->
<template>
  <div class="app-container">
    <el-form ref="form" inline :model="form" class="form-options" label-width="100px">
      <el-form-item label="设备" prop="equName">
        <el-select v-model="form.equName" placeholder="请选择设备类型" filterable clearable>
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开关机状态" prop="runStatus">
        <el-select v-model="form.eqSwitch" placeholder="请选择开关机状态" filterable clearable>
          <el-option
            v-for="item in statusOptions1"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运行状态" prop="runStatus">
        <el-select v-model="form.workingMode" placeholder="请选择运行状态" filterable clearable>
          <el-option
            v-for="item in statusOptions2"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报警状态" prop="runStatus">
        <el-select v-model="form.alarmInfo" placeholder="请选择报警状态" filterable clearable>
          <el-option
            v-for="item in statusOptions3"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="离线状态" prop="runStatus">
        <el-select v-model="form.line" placeholder="请选择离线状态" filterable clearable>
          <el-option
            v-for="item in statusOptions4"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->

      <div class="btns">
        <el-button type="primary" @click="onQuery">查询</el-button>
        <el-button @click="resetFormSearch('form')">重置</el-button>
      </div>
    </el-form>
    <!--列表-->
    <!-- v-loading="loading" -->
    <el-table
      height="600"
      :data="tableData"
      header-cell-class-name="table-header"
      style="width: 100%;margin-top: 20px" 
      show-overflow-tooltip
    >
      <el-table-column
        label="序号"
        type="index"
        width="50"
      />
      <el-table-column
        prop="equName"
        label="设备"
      />
      <el-table-column
        prop="eqSwitch"
        label="开关机状态"
      >
        <template slot-scope="scope">
          <el-image v-if="scope.row.eqSwitch == 1" :src="iconSwitch1" style="width: 22px;height: 22px;"></el-image>
          <el-image v-else-if="scope.row.eqSwitch == 0" :src="iconSwitch0" style="width: 22px;height: 22px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="workingMode"
        label="运行状态"
      >
        <template slot-scope="scope">
          <el-image v-if="scope.row.workingMode == 1" :src="iconWork1" style="width: 22px;height: 22px;"></el-image>
          <el-image v-else-if="scope.row.workingMode == 0" :src="iconWork0" style="width: 22px;height: 22px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="alarmInfo"
        label="报警状态"
      >
        <template slot-scope="scope">
          <el-image v-if="scope.row.alarmInfo == 1" :src="iconAlarm1" style="width: 22px;height: 22px;"></el-image>
          <el-image v-else-if="scope.row.alarmInfo == 0" :src="iconAlarm0" style="width: 22px;height: 22px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="line"
        label="离线状态"
      >
        <template slot-scope="scope">
          {{scope.row.line == 0 ? '在线' : scope.row.line == 1 ? '离线' : ''}}
        </template>
      </el-table-column>
      <el-table-column
        prop="eqWeight"
        label="称重重量(g)"
      />
      <!-- <el-table-column
        prop="runStatus"
        label="状态"
      >
        <template slot-scope="scope">
          <i class="el-icon-s-opportunity" :style="{'font-size': '20px', 'color': scope.row.runStatus == 0 ? 'green' : 'red'}"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="monitorDescribe"
        label="状态描述"
      /> -->
  </el-table>
    <!--分页-->
    <div v-if="total > 0" style="text-align: right;margin-top: 15px">
      <el-pagination
        background
        :current-page="currentPage"
        :page-sizes="[10, 15, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 弹窗 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="20%"
    >
      <el-form ref="diaForm" inline :model="diaForm" :rules="rules" class="form-options">
        <el-form-item label="名称" prop="name">
          <el-input v-model="diaForm.name" placeholder="请输入任务名称" clearable/>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="onSubmit()">保存</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getQueryEquipmentMonitor,
  getDevList
} from '@/api/deviceMonitor';
let timer = null;

export default {
  name: 'DeviceMonitor',
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      typeOptions: [],
      iconSwitch0: require('@/assets/images/icon-switch0.png'),
      iconSwitch1: require('@/assets/images/icon-switch1.png'),
      iconWork0: require('@/assets/images/icon-work0.png'),
      iconWork1: require('@/assets/images/icon-work1.png'),
      iconAlarm0: require('@/assets/images/icon-alarm0.png'),
      iconAlarm1: require('@/assets/images/icon-alarm1.png'),
      statusOptions1: [{
        value: undefined,
        label: '全部'
      }, {
        value: 0,
        label: '关机'
      }, {
        value: 1,
        label: '开机'
      }],
      statusOptions2: [{
        value: undefined,
        label: '全部'
      }, {
        value: 0,
        label: '停止'
      }, {
        value: 1,
        label: '运行中'
      }],
      statusOptions3: [{
        value: undefined,
        label: '全部'
      }, {
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }],
      statusOptions4: [{
        value: undefined,
        label: '全部'
      }, {
        value: 0,
        label: '在线'
      }, {
        value: 1,
        label: '离线'
      }],
      form: {
        equName: '',
        eqSwitch: undefined,
        workingMode: undefined,
        alarmInfo: undefined,
      },
      loading: false,
      tableData: [],
      dialogVisible: false,
      diaForm: {
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getDevListFun();
    this.onQuery();
    timer = setInterval(() => {
      this.onQuery();
    }, 3000)
  },
  destroyed() {
    clearInterval(timer);
  },
  methods: {
    // 分页
    handleSizeChange: function(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getTableData();
    },
    handleCurrentChange: function(val) {
      this.currentPage = val;
      this.getTableData();
    },
    // 接口-设备类型
    getDevListFun() {
      getDevList({
        page: 1,
        rows: 100
      }).then(res => {
        const { resultData } = res;
        resultData.forEach(element => {
          this.typeOptions.push({
            value: element.id,
            label: element.category
          })
        });
      })
    },
    // 查询
    onQuery() {
      console.log('查询', this.form)
      this.currentPage = 1;
      this.getTableData();
    },
    // 如果为空则不传该字段
    checkParams(params) {
      for (let key in params) {
        if (params[key] === null || params[key] === undefined) {
            delete params[key]
        }
      }
      return params;
    },
    // 查询盘库列表数据
    getTableData() {
      const par =  this.checkParams(this.form);
      this.loading = true;
      const params = {
        page: this.currentPage,
        rows: this.pageSize,
        ...par
      };
      getQueryEquipmentMonitor(params).then(res => {
        this.tableData = res.resultData;
        // 测试
        // this.tableData = [{
        //   alarmInfo: 0,
        //   equName: 'gyg',
        //   eqSwitch: 0,
        //   eqWeight: 12,
        //   line: 0,
        //   workingMode: 1
        // }]
        this.total = res.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },
    // 新增
    handleAdd() {
      this.dialogVisible = true;
      this.resetForm("diaForm");
    },
    // 新增提交
    onSubmit() {
      this.$refs["diaForm"].validate((valid) => {
        if (valid) {
          const { name } = this.diaForm;
          getCheckAdd({name}).then(res => {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            this.onQuery();
            this.dialogVisible = false;
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 搜索重置
    resetFormSearch(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
        this.currentPage = 1;
        this.onQuery();
      })
    },
    // 重置
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}
.grid-content {
  margin-top: 20px;
  border-radius: 4px;
  text-align: center;
  min-height: 36px;
  line-height: 36px;
}
.bg-purple {
  background: #e5e9f2;
}
</style>

